"use client"

import { Button } from "@nextui-org/button"
import { toggleTheme } from '@/store/modules/ThemeConfig'
import { useSelector,useDispatch } from "react-redux"

const baseURL = "http://localhost:3000"

const AddCate = () => {
    const dispatch = useDispatch()
    const { isTheme } = useSelector(state => state.theme)

    const add = async () => {
        await fetch(`${baseURL}/cate`, { method: "post", body: JSON.stringify({ title: "新分类" }) })

        const data = await fetch(`${baseURL}/cate`).then(res => res.json())
        console.log(data[data.length - 1]);

        await fetch(`${baseURL}/content`, { method: "post", body: JSON.stringify({ id: data[data.length - 1].id, content: data[data.length - 1].id + "分类的内容" }) })
    }

    return (
        <>
            <h1>{isTheme+''}</h1>
            <Button className="my-[20px]" onClick={add}>新增数据</Button>
            <Button className="my-[20px] ml-10" color="primary" onClick={()=>dispatch(toggleTheme())}>主题切换</Button>
        </>
    )
}

export default AddCate

// 创建两个组件，a和b，分别定义变量a和b中分别页面。然后通过redux修改他